package com.doge.walknovel.api

import androidx.lifecycle.LifecycleOwner
import com.doge.wnpro.R
import com.doge.walknovel.utils.toast
import io.reactivex.observers.DisposableObserver
import java.net.SocketTimeoutException
import java.net.UnknownServiceException

open class ObserverAdapter<T>(owner: LifecycleOwner? = null) : DisposableObserver<T>() {
    init {
        bindLifecycle(owner)
    }

    override fun onComplete() {
    }

    override fun onNext(t: T) {
    }

    override fun onError(e: Throwable) {
        e.printStackTrace()
        if (e is ServerException) {
            toast(e.msg)
        } else if (
            e is retrofit2.adapter.rxjava2.HttpException ||
            e is retrofit2.HttpException
        ) {
            toast(e.message.toString())
        } else if (e is SocketTimeoutException) {
            toast(R.string.request_timeout)
        } else if (e is java.net.UnknownHostException || e is UnknownServiceException) {
            toast(R.string.unable_to_connect_to_server)
        }
    }
}


